<?php
  
  session_start();

  include('includes/image-function.php');
  $tipe = $_POST['tipe'];
  switch($_GET['act']){
      case 1 : //INSERT MENU BARU
              $error = "";
              $msg = "";
              $fileElementName = 'file-to-upload';
              if(!empty($_FILES[$fileElementName]['error'])){
                switch($_FILES[$fileElementName]['error']){

                  case '1':
                    $error = 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
                    break;
                  case '2':
                    $error = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
                    break;
                  case '3':
                    $error = 'The uploaded file was only partially uploaded';
                    break;
                  case '4':
                    $error = 'No file was uploaded.';
                    break;

                  case '6':
                    $error = 'Missing a temporary folder';
                    break;
                  case '7':
                    $error = 'Failed to write file to disk';
                    break;
                  case '8':
                    $error = 'File upload stopped by extension';
                    break;
                  case '999':
                  default:
                    $error = 'No error code avaiable';
                }
              }elseif(empty($_FILES['file-to-upload']['tmp_name']) || $_FILES['file-to-upload']['tmp_name'] == 'none'){
                $error = 'No file was uploaded..';
              }else{

                $filename = $_FILES['file-to-upload']['tmp_name'];

                $data = getRatio($filename);


                $fileName = $_FILES['file-to-upload']['name'];
                $fileSize = $_FILES['file-to-upload']['size'];
                $fileError = $_FILES['file-to-upload']['error'];
                
                $data = newCustomImg($filename, $data['ratio'], $data['orientation'], 960);
                if ($tipe == 2) {
                  imagejpeg($data['canvas'], 'img/group/resize_'.$_GET["id"].'.jpg', 90);
                } else {
                  imagejpeg($data['canvas'], 'img/resize_'.$_GET["id"].'.jpg', 90); 
                }
              }   
              echo "{";
              echo        "error: '" . $error . "',\n";
              if ($tipe == 2) {
                echo        "url: 'img/group/resize_" . $_GET["id"] . ".jpg?".time()."',\n";
              } else{
                echo        "url: 'img/resize_" . $_GET["id"] . ".jpg?".time()."',\n";
              }
              echo        "curHeight: '" . $data['height'] . "',\n";
              echo        "curWidth: '" . $data['width'] . "'\n";
              echo "}";
            break;
      case 2:
          $x1 = $_POST["x1"];
          $y1 = $_POST["y1"];
          $x2 = $_POST["x2"];
          $y2 = $_POST["y2"];
          $w = $_POST["w"];
          $h = $_POST["h"];
          $oriW = $_POST["oriW"];
          $oriH = $_POST["oriH"];
          //Scale the image to the thumb_width set above
          if ($tipe == 2) {
            $img1 = resizeThumbnailImage("img/group/resize_".$_GET["id"].".jpg",$w,$h,$x1,$y1,$oriW,$oriH,48);
            imagejpeg($img1, 'img/group/ico_'.$_GET["id"].'.jpg', 90);

            $img2 = resizeThumbnailImage("img/group/resize_".$_GET["id"].".jpg",$w,$h,$x1,$y1,$oriW,$oriH,256);
            imagejpeg($img2, 'img/group/thumb_'.$_GET["id"].'.jpg', 90);
          } else{
            $img1 = resizeThumbnailImage("img/resize_".$_GET["id"].".jpg",$w,$h,$x1,$y1,$oriW,$oriH,48);
            imagejpeg($img1, 'img/ico_'.$_GET["id"].'.jpg', 90);

            $img2 = resizeThumbnailImage("img/resize_".$_GET["id"].".jpg",$w,$h,$x1,$y1,$oriW,$oriH,256);
            imagejpeg($img2, 'img/thumb_'.$_GET["id"].'.jpg', 90);
          }

        break;
      }
?>